package com.cenntro.nearr.common;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;

public class Result implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3599792303100774814L;

	public static final int SUCCESS = 0;
	public static final int ERROR = 1;
	/**
	 * 返回是否成功的状态 0:表示成功 1或其他值代表失败
	 */
	private int state;
	/**
	 * 成功时,返回的JSON数据
	 */
	private Object data;
	/**
	 * 存储提示信息
	 */
	private String message;
	public Result() {
	}
	public Result(int state, Object data, String message) {
		this.state = state;
		this.data = data;
		this.message = message;
	}

	public Result(int state, Throwable e) {
		this.state = state;
		data = null;
		message = e.getMessage();
	}

	public static Result ok(Object data) {
		return new Result(SUCCESS, data, null);
	}
	public static Result ok(Object data, String msg) {
		return new Result(SUCCESS, data, msg);
	}
	public static Result ok(String ...msg) {
		return new Result(SUCCESS, null, StringUtils.join(msg));
	}
	
	public static Result error(String msg, Object... data) {
		return new Result(ERROR, data, msg);
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public String toString() {
		return "result [state=" + state + ", data=" + data + ", message=" + message + "]";
	}
	
	public static void main(String[] args) {
		System.out.println(Result.ok(""));
	}

}
